#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Output a result as a long/expanded record.
"""

import utility
import urlcheck
import urllib
import re
import querymgr
import doctype

# import local modules
try:
	from mod_python import apache
	recordfunctions = apache.import_module('recordfunctions')
except:
	import recordfunctions

def outlong(result, vars, req):
	# prepare output for title
	
	validurl = vars['validurl']
	boolquerystring = vars['boolquerystring']
	searchraw = vars['searchraw']
	pageresultnum = vars['pageresultnum']
	termslist = vars['termslist']

	# create url to send to tracker
	trackurl = utility.maketrackurl(validurl, result['recHash'], boolquerystring)

	# backup title - need clean copy for possible google search
	googletitle = result['mTitle']

	# verify title present
	if len(result['mTitle'].strip()) < 1:
		result['mTitle'] = '[No Title]'

	# add line breaks where needed
	result['mTitle'] = result['mTitle'].replace('\n', '<br>')

	# bold generic keywords and title keywords
	result['mTitle'] = utility.boldify(result['mTitle'], termslist['keywords'] + termslist['mTitle'])

	# if url is present
	if len(validurl) > 0:
		# add url click tracker script
		scriptrecord = """onclick='return recordClick("%s","%s");'""" % (result['recHash'], urllib.quote_plus(searchraw))

		# add link anchor tag
		result['mTitle'] = '<a %s href="%s">%s</a>' % (scriptrecord,validurl, result['mTitle'])

	# prepare output for creator

	# parse creators, provide quick author searches for each
	creators = recordfunctions.parsecreator(result['mCreator'])

	# add line breaks where needed
	result['mCreator'] = result['mCreator'].replace('\n', '<br>')

	# bold search words
	creators = utility.boldify(creators, termslist['keywords'] + termslist['mCreator'])

	# link the creators to quick searches
	result['mCreator'] = recordfunctions.linkcreator(result['mCreator'], creators).strip()

	# prepare the output for subject

	# add line breaks where needed
	result['mSubject'] = result['mSubject'].replace('\n', '<br>')

	# bold generic keywords and subject keywords
	result['mSubject'] = utility.boldify(result['mSubject'], termslist['keywords'] + termslist['mSubject'])

	# prepare output for description

	# add line breaks where needed
	result['mDescription'] = result['mDescription'].replace('\n', '<br>')

	# bold generic keywords and description keywords
	result['mDescription'] = utility.boldify(result['mDescription'], termslist['keywords'] + termslist['mDescription'])

	# add links to description
	result['mDescription'] = urlcheck.linkify(result['mDescription'])

	# output

	# start the table
	req.write("""<table border="0" class="smallish">""")

	### TITLE
	req.write("""<tr><td class="expand_bg">Title:</td><td>""")
	req.write("""%s</td></tr>""" % result['mTitle'])

	### ACTIONS
	req.write("""<tr><td class="expand_bg">Actions:</td><td>""")
	data = (result['recHash'], pageresultnum, urllib.quote_plus(searchraw), result['recHash'], result['recHash'])
	req.write("""<a title="View Abreviated Metadata" 
		onClick="sendShrink('%s','res%s','%s');">
		<span class="details">Shrink</span></a>
		<![if gt IE 7]>&nbsp; <a 
		onClick="addTab('%s'); rememberTab('%s'); sendRemember(); checkMove(0);">
		<span class="details">Remember</span></a><![endif]>
		""" % data)

	req.write("""</td></tr>""")

	### CREATOR
	# verify length
	if len(result['mCreator']) > 2:
		# print creator
		req.write("""<tr><td class="expand_bg">Creator/Author:</td><td>""")
		req.write("""<i>%s</i>""" % result['mCreator'])
		req.write("""</td></tr>""")

	### SUBJECT

	# verify length
	if len(result['mSubject']) > 2:
		# print subject
		req.write("""<tr><td class="expand_bg">Subject:</td><td>""")
		req.write("""<span class="grey">%s</span>""" % result['mSubject'])
		req.write("""</td></tr>""")

	### PUBLISHER
	req.write( recordfunctions.outfieldlong('Publisher', result['mPublisher']) )

	### CONTRIBUTOR
	req.write( recordfunctions.outfieldlong('Contributor(s)', result['mContributor']) )

	### REPOSITORY
	req.write( recordfunctions.outfieldlong('Repository', result['iRepository']) )

	### DATE
	req.write( recordfunctions.outfieldlong('Date', result['mDate']) )

	### DESCRIPTION

	# out description
	if not utility.empty(result['mDescription']):
		# print description
		req.write("""<tr><td class="expand_bg">Description:</td><td>""")
		req.write( result['mDescription'] )
		req.write("""</td></tr>""")

	### RIGHTS
	req.write( recordfunctions.outfieldlong('Rights', result['mRights']) )

	### TYPE
	req.write( recordfunctions.outfieldlong('Type', result['mType']) )

	### RELATION
	req.write( recordfunctions.outfieldlong('Relation', result['mRelation']) )

	### COVERAGE
	req.write( recordfunctions.outfieldlong('Coverage', result['mCoverage']) )

	### LANGUAGE
	req.write( recordfunctions.outfieldlong('Language', result['mLanguage']) )

	### SOURCE
	req.write( recordfunctions.outfieldlong('Source', result['mSource']) )

	### FORMAT
	# format icon parse
	#doctypeicon = doctype.gettypeicon(result['mFormat'])
	
	req.write( recordfunctions.outfieldlong('Format', result['mFormat']) )
	
	# print doctype icon
	#req.write( doctypeicon )

	### IDENTIFIER 1 (header)
	req.write( recordfunctions.outfieldlong('Identifier 1', result['hIdentifier']) )

	### IDENTIFIER 2 (metadata)
	req.write( recordfunctions.outfieldlong('Identifier 2', result['mIdentifier']) )

	### URL
	req.write("""<tr><td class="expand_bg">URL:</td><td>""")
	# greenify it
	req.write("""<span class="greenify" """)

	# if there was no valid url found, allow for a google scholar search
	if validurl == '':
		# get the title and clean it up a bit if possible
		googletitle = utility.cleantitle(googletitle)

		# generate the google scholar link
		req.write("""><a
			href="http://scholar.google.com/scholar?q=%%22%s%%22"
			>Search Google Scholar</a>
			""" % urllib.quote_plus(googletitle) )

		# also normal google search
		req.write("""&nbsp;&nbsp;<a
			href="http://www.google.com/search?q=%%22%s%%22"
			>Search Google</a>
			""" % urllib.quote_plus(googletitle) )

	else:
		# if valid url is present, output it
		req.write(""">%s""" % validurl)

	req.write("""</span></td></tr>""")

	### DATESTAMP

	# display datestamp only if present
	if not utility.empty(result['hDatestamp']):
		req.write("""<tr><td class="expand_bg">Datestamp:</td><td>""")
		req.write("""<span class="greenify">""")
		
		dateMan = re.search('(.+)(T.*)$', result['hDatestamp'])

		# print datestamp
		req.write( result['hDatestamp'] )
		req.write("""</span></td></tr>""")


	req.write("""</table>""")

	### TAGGING

	# tag
	tagnum = 1

	# tag container div
	req.write("""<div id="tagcontainer%s" class="tagbox tagindent">""" % pageresultnum)

	tags = []

	# loop over tags for record
	for tag in tags:
		pass
		tagnum = tagnum + 3

	# after existing tags are output, add one more row for additional tags
	req.write("""
		<div>
			<input type="text" name="tag%s"
			size="20" maxlength="30"
			onkeyup="verifyTag(this);"
			value="">

			<input type="text" name="tag%s"
			size="20" maxlength="30"
			onkeyup="verifyTag(this);"
			value="">

			<input type="text" name="tag%s"
			size="20" maxlength="30"
			onkeyup="verifyTag(this);"
			value="">
		</div>
		""" % (tagnum, tagnum+1, tagnum+2) )
	

	# end tag for taglist
	req.write("""<div id="endtag%s"></div>""" % pageresultnum)

	# close tag container
	req.write("""</div>""")

	# tag actions div
	req.write("""<div id="tagactions%s" class="smallish tagindent">""" % pageresultnum)

	# tag actions contents
	req.write("""<a class="fakelink" onclick='setTags(%s);'>Set Tags</a>""" % pageresultnum)

	# close tag actions
	req.write("""</div>""")


# eof

